<script type="text/javascript">
    
    window.onload = function() {
        var product = dojo.byId('LoanProductId'); 
        product.onchange = function () {
            updateLoanProductFields('/admin/json/get-loan-product-data/id/' + product.value);
        };
        var line = dojo.byId('ProductLineId');
        line.onchange = function () { 
            updateOpts('ProgramId','/admin/json/get-line-program-options/id/' + line.value); 
            /*
            product.options.length = 0; 
            updateLoanProductFields('/admin/json/get-loan-product-data/id/0');
            */
            return false; 
        };     
        var program = dojo.byId('ProgramId');
        program.onchange = function () {  
            /*
            updateLoanProductFields('/admin/json/get-loan-product-data/id/0');
            updateOpts('LoanProductId','/admin/json/get-program-product-options/id/' + program.value);
            */
            return false; 
        };
        <?php foreach($this->Borrower->Clients as $client) : ?>
        var clientId<?php echo $client->ClientId  ?> = dojo.byId('NoLoan-ClientId<?php echo $client->ClientId  ?>');
        clientId<?php echo $client->ClientId  ?>.onchange = function () {
            setPrincipal('<?php echo $client->ClientId  ?>');
            return false;
        };
        if(clientId<?php echo $client->ClientId  ?>.checked) 
            setPrincipal('<?php echo $client->ClientId  ?>');
        <?php endforeach; ?>
    };
    
    function setPrincipal(client) 
    {
        if(dojo.byId('NoLoan-ClientId' + client).checked) {
                dojo.byId('Principal-' + client).value = '0.00';
                dojo.attr('Principal-' + client,'readonly','readonly');
        } else {
                dojo.byId('Principal-' + client).value = '';
                dojo.attr('Principal-' + client,'readonly',false);  
        }
    }
    
    function updateOpts(tg,url,callback)
    {
        dojo.xhrGet({
            url: url,
            handleAs: "json",
            timeout: 5000,
            load: function(response, ioArgs) {                
                var obj = dojo.byId(tg);
                obj.options.length = 0;                
                for (index = 0; index < response.length; ++index) {
                    option = response[index];
                    obj.options.add(new Option(option.oT, option.oV));
                }   
                callback();
                return response;
            },
            error: function(response, ioArgs) {
                //alert('Failed xhrGet');
                return response;
            }
        });
    }
    
    function updateLoanProductFields(url,callback)
    {
        var cols = {
            'AnnualPercentageRate':true,'AnnualPercentageRateMin':true,'AnnualPercentageRateMax':true,
            'LoanInsurancePct':true,'LoanInsurancePctMin':true,'LoanInsurancePctMax':true,
            'HealthInsurancePct':true,'HealthInsurancePctMin':true,'HealthInsurancePctMax':true,
            'AdministrativeFeePct':true,'AdministrativeFeePctMin':true,'AdministrativeFeePctMax':true,
            'PrincipalGraceRate':true,'PrincipalGraceRateMin':true,'PrincipalGraceRateMax':true,
            'DelayPenaltyRate':true,'DelayPenaltyRateMin':true,'DelayPenaltyRateMax':true,
            'DelayPenaltyFixedCharge':true,'DelayPenaltyFixedChargeMin':true,'DelayPenaltyFixedChargeMax':true,
            'InterestCalculation':true,'Installments':true,'CalendarType':true,'PrincipalGraceMargin':true,
            'DelayGrace':true,'PaymentFrequency':true
        };
        dojo.xhrGet({
            url: url,
            handleAs: "json",
            timeout: 5000,
            load: function(response, ioArgs) {
                for (index = 0; index < response.length; ++index) { 
                    if(cols[response[index].oV]) {
                        dojo.byId(response[index].oV).value = response[index].oT;
                    }
                }
                callback();
                return response;
            },
            error: function(response, ioArgs) {
                //alert('Failed xhrGet');
                return response;
            }
        });
    }
</script>

<style>
    .zend_form_custom dt { clear:none; width:45px; }  
    .zend_form_custom input[type=text] { width: 120px; } 
    .zend_form_custom input[type=checkbox] { margin: 0; width: 13px; position: relative; top: 2px;} 
</style>

<form id="Application" 
      enctype="application/x-www-form-urlencoded" 
      method="<?php echo $this->form->getMethod() ?>" 
      title="<?php echo $this->form->getAttrib("title") ?>" 
      action="<?php echo $this->form->getAction() ?>">
    
    <?php echo $this->form->getElement('LocationId') ?>
    <?php echo $this->form->getElement('OfficerUserId') ?>
    <?php echo $this->form->getElement('BorrowerType') ?>
    <?php echo $this->form->getElement('BorrowerId') ?>

    <div style="height: auto !important; position: absolute; left: 10px; top: 16px; bottom: 130px; overflow-y: hidden;">
        <dl class="zend_form">
            <dt><?php echo $this->translate("Borrower") ?></dt>
            <dd><?php echo $this->Borrower->Name ?><br />
                <?php echo $this->Borrower->Code ?></dd>
            <dt><?php echo $this->translate("Location") ?></dt>
            <dd><?php echo $this->Borrower->Location ?></dd> 
            <dt><?php echo $this->translate("Community") ?></dt>
            <dd><?php echo $this->Borrower->Community ?></dd> 
            <dt><?php echo $this->translate("Officer") ?></dt>
            <dd><?php echo $this->Borrower->Officer ?></dd> 
            <?php echo $this->form->getElement('TentativeDeliveryDate') ?>
            <?php echo $this->form->getElement('TentativeFirstPaymentDate') ?>
            <?php echo $this->form->getElement('ProductLineId') ?>
            <?php echo $this->form->getElement('ProgramId') ?>
            <?php echo $this->form->getElement('LoanProductId') ?>
        </dl>
    </div>
    
    <div style="height: auto !important; position: absolute; left: 500px; top: 16px; bottom: 130px; overflow-y: hidden;">     
        <dl class="zend_form">
            <dt></dt>
            <dd class="matrixRow">
                <div class="label"><?php echo $this->translate("Min")  ?></div>
                <div class="label"><?php echo $this->translate("Std")  ?></div>
                <div class="label"><?php echo $this->translate("Max")  ?></div>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('AnnualPercentageRate')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('AnnualPercentageRateMin')  ?>
                <?php echo $this->form->getElement('AnnualPercentageRate')  ?>
                <?php echo $this->form->getElement('AnnualPercentageRateMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('AnnualPercentageRate')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('LoanInsurancePct')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('LoanInsurancePctMin')  ?>
                <?php echo $this->form->getElement('LoanInsurancePct')  ?>
                <?php echo $this->form->getElement('LoanInsurancePctMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('LoanInsurancePct')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('HealthInsurancePct')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('HealthInsurancePctMin')  ?>
                <?php echo $this->form->getElement('HealthInsurancePct')  ?>
                <?php echo $this->form->getElement('HealthInsurancePctMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('HealthInsurancePct')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('AdministrativeFeePct')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('AdministrativeFeePctMin')  ?>
                <?php echo $this->form->getElement('AdministrativeFeePct')  ?>
                <?php echo $this->form->getElement('AdministrativeFeePctMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('AdministrativeFeePct')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('PrincipalGraceRate')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('PrincipalGraceRateMin')  ?>
                <?php echo $this->form->getElement('PrincipalGraceRate')  ?>
                <?php echo $this->form->getElement('PrincipalGraceRateMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('PrincipalGraceRate')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('DelayPenaltyRate')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('DelayPenaltyRateMin')  ?>
                <?php echo $this->form->getElement('DelayPenaltyRate')  ?>
                <?php echo $this->form->getElement('DelayPenaltyRateMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('DelayPenaltyRate')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt class="required"><?php echo $this->form->getElement('DelayPenaltyFixedCharge')->getLabel() ?></dt>
            <dd class="matrixRow">
                <?php echo $this->form->getElement('DelayPenaltyFixedChargeMin')  ?>
                <?php echo $this->form->getElement('DelayPenaltyFixedCharge')  ?>
                <?php echo $this->form->getElement('DelayPenaltyFixedChargeMax') ?>
            </dd>
            <dt></dt>
            <dd>
                <ul class="errors">
                <?php foreach($this->form->getElement('DelayPenaltyFixedCharge')->getMessages() as $error): ?>
                    <li><?php echo $error; ?></li>
                <?php endforeach; ?>
                </ul>
            </dd>
            <dt></dt>
            <?php echo $this->form->getElement('InterestCalculation') ?>
            <?php echo $this->form->getElement('PaymentFrequency') ?>
            <?php echo $this->form->getElement('Installments') ?>
            <?php echo $this->form->getElement('CalendarType') ?>
            <?php echo $this->form->getElement('PrincipalGraceMargin') ?>
            <?php echo $this->form->getElement('DelayGrace') ?>
        </dl>
    </div>
    
    <div class="vgrid" style="height: auto !important; min-height: 100px; bottom: 0px;">
        <br />
        <dl class="zend_form zend_form_custom">
        <?php foreach($this->Borrower->Clients as $client) : ?>
        <dt><img style="border: 1px solid #bcd; margin-top: 5px;"  src="/default/photo/thumb/cat/i/id/<?php echo $client->ClientId ?>" /></dt>
        <dd style="width:200px; margin-top: 5px;">
            <?php echo $client->FullName ?><br />
            <?php echo $client->Code ?><br />
            <?php echo $this->form->getElement('ClientId'.$client->ClientId)  ?>
                <?php echo $this->translate("Client is skipping loan") ?><br />
            <?php echo $this->form->getElement($client->ClientId)  ?>
            <ul class="errors">
            <?php foreach($this->form->getElement($client->ClientId)->getMessages() as $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
            </ul>
        </dd>
        <?php endforeach; ?>
        </dl>
    </div>
    
    <div style="position: absolute; left: 980px; top: 30px;">
        <?php echo $this->form->getElement('Submit') ?>
    </div>
    
</form>